﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace Cards
{
    class Input
    {
        #region Variables

        public int position = 1;
        public static int card = 1;
        bool keyPress = true;
        public bool inHand = true;
        public int cardSelection;

        //holds the selected card from your hand and the selected spot on the board
        public int[] cardAndSpotSelection;

        //Texture for cursor
        Texture2D cursorTex;

        //Position of cursor
        public Vector2[] cursorPosition;

        //font
        SpriteFont font;

        //the player selected the same card in hand and on board
        public bool drawCompareOutput;

        //the player selected the same card in hand and on board
        public bool selectedCorrect;

        #endregion

        //initializes variables
        public Input(ContentManager Content)
        {
            //holds the x,y coordinates of the cursor
            cursorPosition = new Vector2[113];

            cardAndSpotSelection = new int[2];

            //initialize booleans
            selectedCorrect = false;
            drawCompareOutput = false;


            //initializes the positions for cursor over the board items
            int location = 1;
            for (int y = 1; y <= 10; y++)
            {
                for (int x = 1; x <= 10; x++)
                {
                    cursorPosition[location].X = x * 64;
                    cursorPosition[location].Y = y * 40;
                    location++;
                }
            }

            //adds positions for cursor when over hand items
            for (int i = 101; i <= 106; i++)
            {
                cursorPosition[i] = new Vector2(((i-100) * 65), 480);
                cursorPosition[i + 6] = new Vector2(((i - 100) * 65), 520);
            }

            //cursorPosition[1] = new Vector2(64, 40);
            //cursorPosition[2] = new Vector2(64 * 2, 40);
            //cursorPosition[3] = new Vector2(64 * 3, 40);
            //cursorPosition[4] = new Vector2(64 * 4, 40);
            //cursorPosition[5] = new Vector2(64 * 5, 40);
            //cursorPosition[6] = new Vector2(64 * 6, 40);
            //cursorPosition[7] = new Vector2(64 * 7, 40);
            //cursorPosition[8] = new Vector2(64 * 8, 40);
            //cursorPosition[9] = new Vector2(64 * 9, 40);
            //cursorPosition[10] = new Vector2(64 * 10, 40*2);
            //cursorPosition[11] = new Vector2(64, 40*2);
            //cursorPosition[12] = new Vector2(64 * 2, 40*2);

            cursorTex = Content.Load<Texture2D>("selector");
            font = Content.Load<SpriteFont>("Font");
        }

        public void Update(SpriteBatch spriteBatch)
        {

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();


            //Draw corsur around the cards if we ar in the hand
            if (!inHand)
                spriteBatch.Draw(cursorTex, cursorPosition[position], Color.Green);
            //otherwise draw it around the board cards
            else
            {
                if (Game1.isPlayer1Turn)
                    spriteBatch.Draw(cursorTex, cursorPosition[100 + card], Color.Green);
                else
                    spriteBatch.Draw(cursorTex, cursorPosition[100 + card + 6], Color.Green);
            }

            //draw whether or not the comparison was right
            if (drawCompareOutput)
            {
                if (selectedCorrect)
                    spriteBatch.DrawString(font, "Correct selection", new Vector2(400, 525), Color.Blue);
                else spriteBatch.DrawString(font, "Not correct selection", new Vector2(400, 525), Color.Blue);
            }
            spriteBatch.End();
        }

        public void GetBoardInput(Deck board)
        {
            // Get input state.
            KeyboardState keyboardState = Keyboard.GetState();

            #region Left Key
            //Left key press
            if (keyboardState.IsKeyDown(Keys.Left) && keyPress && position >= 1)
            {
                //Checks to see if position is at the left edge, if it is it wraps to right side
                if (position == 1 || position == 11 || position == 21 || position == 31 || 
                        position == 41 || position == 51 || position == 61 || position == 71 || 
                        position == 81 || position == 91)
                {
                    position += 9;
                }
                else
                {
                    position -= 1;
                }

                keyPress = false;
            }
            #endregion

            #region Right Key
            //Right key press
            if (keyboardState.IsKeyDown(Keys.Right) && keyPress && position <= 100)
            {
                //Checks to see if position is at the right edge, if it is it wraps to the left side
                if (position == 10 || position == 20 || position == 30 || position == 40 ||
                        position == 50 || position == 60 || position == 70 || position == 80 ||
                        position == 90 || position == 100)
                {
                    position -= 9;
                }
                else
                {
                    position += 1;
                }

                keyPress = false;
            }
            #endregion

            #region Up Key
            //Up key press
            if (keyboardState.IsKeyDown(Keys.Up) && keyPress)
            {
                //Checks to see if position is at the top edge, if it is it wraps to the bottom edge
                if (position == 1 || position == 2 || position == 3 || position == 4 ||
                        position == 5 || position == 6 || position == 7 || position == 8 ||
                        position == 9 || position == 10)
                {
                    position += 90;
                }
                else
                {
                    position -= 10;
                }

                keyPress = false;
            }
            #endregion

            #region Down Key
            //Down key press
            if (keyboardState.IsKeyDown(Keys.Down) && keyPress)
            {
                //Checks to see if position is at the bottom edge, if it is it wraps to the top edge
                if (position == 91 || position == 92 || position == 93 || position == 94 ||
                        position == 95 || position == 96 || position == 97 || position == 98 ||
                        position == 99 || position == 100)
                {
                    position -= 90;
                }
                else
                {
                    position += 10;
                }

                keyPress = false;
            }
            #endregion

            if (keyboardState.IsKeyDown(Keys.B))
            {
                inHand = true;
                keyPress = false;
                cardAndSpotSelection[1] = board.deck[position];
                Compare();
                card = 1;
            }

            #region Key Press Reset
            if (keyboardState.IsKeyUp(Keys.Left) && keyboardState.IsKeyUp(Keys.Right) &&
                    keyboardState.IsKeyUp(Keys.Up) && keyboardState.IsKeyUp(Keys.Down) && !keyPress)
            {
                keyPress = true;
            }
            #endregion
        }
        
        public void GetHandInput(int[] hand)
        {
            // Get input state.
            KeyboardState keyboardState = Keyboard.GetState();

            #region Left Key
            if (keyboardState.IsKeyDown(Keys.Left) && keyPress && card >= 1)
            {
                drawCompareOutput = false;
                if (card == 1)
                {
                    card = 6;
                }
                else
                {
                    card -= 1;
                }

                keyPress = false;
            }
            #endregion

            #region Right Key
            if (keyboardState.IsKeyDown(Keys.Right) && keyPress && card <= 6)
            {
                drawCompareOutput = false;
                if (card == 6)
                {
                    card = 1;
                }
                else
                {
                    card += 1;
                }

                keyPress = false;
            }
            #endregion

            if (keyboardState.IsKeyDown(Keys.A) && keyPress)
            {
                cardSelection = card;
                inHand = false;
                keyPress = false;
                cardAndSpotSelection[0] = hand[card];
            }
            
            if (keyboardState.IsKeyDown(Keys.X) && keyPress)
            {
                drawCompareOutput = false;
            }


            #region Key Press Reset
            if(keyboardState.IsKeyUp(Keys.Left) && keyboardState.IsKeyUp(Keys.Right) && keyboardState.IsKeyUp(Keys.A))
            {
                keyPress = true;
            }
            #endregion
        }

        public void Compare()
        {
            if (cardAndSpotSelection[0] == cardAndSpotSelection[1])
            {
                selectedCorrect = true;
                if (Game1.isPlayer1Turn)
                {
                    Game1.playDeck.DealHand(Game1.playDeck, Game1.player1Hand, card);
                    Board.chips[position] = Board.chipState.p1;
                }
                else 
                {
                    Game1.playDeck.DealHand(Game1.playDeck, Game1.player2Hand, card);
                    Board.chips[position] = Board.chipState.p2;
                }

                Game1.isPlayer1Turn = !Game1.isPlayer1Turn;
            }
            else selectedCorrect = false;
            drawCompareOutput = true;
        }        
    }
}